//
//  OptionItem.swift
//  Automobile
//
//  Created by erkebulan elzhan on 8/3/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit


final class OptionItem: UIView {
    
    // MARK: - Outlets
    
    private lazy var expandImg: UIImageView = {
        let img = UIImageView()
        img.image = UIImage(named: "down")
        img.contentMode = .center
        return img
    }()
    private lazy var titleLbl: UILabel = {
        let lbl = UILabel()
        lbl.font = UIFont.systemFont(ofSize: 15, weight: .regular)
        lbl.textAlignment = .left
        lbl.textColor = .darkGray
        return lbl
    }()
    private lazy var checkBox: ViewWithImage = {
        let view = ViewWithImage()
        view.layer.cornerRadius = 4.0
        view.layer.borderWidth = 1
        view.layer.borderColor = UIColor.greyBlue.cgColor
        return view
    }()

    private var checkBoxSelected: Bool = false
    public var isCarCreate: Bool = false
    
    
    init(customSize: Bool = false, showArrow: Bool = true) {
        super.init(frame: .zero)
        self.clipsToBounds = true
        self.layer.cornerRadius = 10
        self.addTitle(spacing: showArrow ? 53 : 16)
        if (!customSize) {
            self.snp.makeConstraints { (make) in
                make.height.equalTo(64)
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - Methods
    
    private func selectCheckBox () {
        
        if (isCarCreate) {
            self.backgroundColor = .darkGray
        }
        
        checkBox.backgroundColor = UIColor(red: 47/255, green: 130/255, blue: 210/255, alpha: 1)
        checkBox.layer.borderColor = UIColor.clear.cgColor
        checkBox.addIcon(icon: UIImage(named: "correct"))
    }
    private func deselectCheckBox () {
        
        if (isCarCreate) {
            self.backgroundColor = .lightBlueColor
        }
        
        checkBox.backgroundColor = .clear
        checkBox.layer.borderColor = UIColor.greyBlue.cgColor
        checkBox.addIcon(icon: nil)
    }

    private func applyMainStyle () {
        if (!isCarCreate) {
            assert(!isCarCreate, "FUCK")
        }
        self.backgroundColor = isCarCreate ? .lightBlueColor : .white
        self.layer.borderWidth = 0.0
    }
    
    private func applyItemStyle () {
        self.backgroundColor = isCarCreate ? .lightBlueColor : .clear
        self.layer.borderWidth = isCarCreate ? 0 : 1.0
        self.layer.borderColor = UIColor.rgb(229, 233, 242).cgColor
    }
    
    
    
    private func addTitle (spacing: CGFloat) {
        self.addSubview(titleLbl)
        titleLbl.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview().inset(spacing)
            make.trailing.equalToSuperview().inset(48)
        }
    }
    private func addArrow () {
        self.addSubview(expandImg)
        expandImg.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview().offset(16)
            make.width.equalTo(8)
            make.height.equalTo(13)
        }
    }
    private func addCheckBox () {
        self.addSubview(checkBox)
        checkBox.snp.makeConstraints { (make) in
            make.width.height.equalTo(16)
            make.trailing.equalToSuperview().inset(16)
            make.centerY.equalToSuperview()
        }
    }
}

// MARK: - Methods

extension OptionItem {
    
    func initialize (isSelected: Bool?=false,
                     isSingle: Bool,
                     title: String,
                     isMain: Bool,
                     textColor: UIColor = .greyColor) {
        self.titleLbl.text = title
        self.titleLbl.textColor = textColor
        
        if (isSingle) {
            if isMain {
                self.applyMainStyle()
            } else {
                self.applyItemStyle()
            }
            expandImg.removeFromSuperview()
            
            if (!isCarCreate) {
                self.addCheckBox()
            }
            
            checkBoxSelected = isSelected ?? false
            checkBox.addIcon(icon: (isSelected ?? false) ? UIImage(named: "correct") : nil )
        } else {
            self.applyMainStyle()
            checkBox.removeFromSuperview()
            self.addArrow()
        }
    }
    
    func updateSelection (isSelected: Bool) {
        self.checkBoxSelected = isSelected
        if (isSelected) {
            self.selectCheckBox()
        } else {
            self.deselectCheckBox()
        }
    }
    
    func updateExpand (isExpanded: Bool) {
        self.expandImg.transform = !isExpanded ? .identity : CGAffineTransform(rotationAngle: .pi)
    }
    
    func toggleSelection () {
        if (!checkBoxSelected) {
            self.selectCheckBox()
        } else {
            self.deselectCheckBox()
        }
        checkBoxSelected.toggle()
    }
    
    func getValue () -> Bool {
        return self.checkBoxSelected
    }
}




final class OptionTap: UITapGestureRecognizer {
    var indexPath: IndexPath!
    var optionIndex: Int?
}
